<html>
<head>

<title>Groovy Goodness: Create Stubs for Testing</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Create Stubs for Testing</h3>

<div class="post">
<p>We can write unit tests with Groovy by simply extending the <code>GroovyTestCase</code> class. In the test we can use the default Groovy classes <code>MockFor</code> and <code>StubFor</code> to create mocks and stubs for classes and interfaces. We can create a mock or stub for Groovy classes, but also for Java classes. Once we have a mock or stub we can execute the code we want to test in the context of the mock or stub with the <code>use()</code> method.</p>
<p>In this example we create a stub for a Groovy service class. We want to define our own return result for the method <code>findLanguages()</code>, so we can test the logic in the caller class.</p>
<pre class="brush:groovy">
import groovy.mock.interceptor.*

class CallServiceTest extends GroovyTestCase {
    def callService = new CallService()
    
    void testServiceCall() {
        def stub = new StubFor(MyService.class)
        stub.demand.findLanguages { ['Java', 'Groovy'] }  // Return one language with an 'a' and one without.
        stub.use {
            callService.myService = new MyService()
            def result = callService.findLanguagesWithA()  // Will use method from stub.
            assert 1 == result.size()
            assert 'Java' == result[0]
        }
    }
    
    void testEmptyResult() {
        def stub = new StubFor(MyService.class)
        stub.demand.findLanguages { ['Groovy', 'Clojure'] }  // Return no languages with an 'a'.
        stub.use {
            callService.myService = new MyService()
            def result = callService.findLanguagesWithA()  // Method from stub is invoked.
            assert !result
        }
    }
}

class CallService {
    def myService = new MyService()
    
    List findLanguagesWithA() {
        def languages = myService.findLanguages()
        languages.grep({ it.contains('a') })
    }
}
class MyService {
    List findLanguages() {
        // Get real data from database for example.
        // That is why we want to stub this class, so we don't rely on
        // the database.
        []
    }
}
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>